package com.dx.mes.service.tool.service;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.E_MTPDLYCHK_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_MTPDLYCHK;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_tool_dly_chk;
import com.dx.common.table.ppt.Ret_tool_dly_chkId;
import com.dx.mes.common.dao.IRetToolDlyChkDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.tool.service.IMtpdailychkService;
import com.dx.mes.facade.tool.tx.Mbprepmag.MbprepmagI;
import com.dx.mes.facade.tool.tx.Mtpdlychk.MtpdlychkI;
import com.dx.mes.facade.tool.tx.Mtpdlychk.MtpdlychkO;

/**
 * Created by ADMIN on 2017/12/4.
 */
@Service("mtpdailychkService")
public class MtpdailychkService implements IMtpdailychkService {
    private static final String TX_NAME = T_MTPDLYCHK;
    private LogUtils logUtils;
    @Autowired
    private IRetToolDlyChkDao retToolDlyChkDao;

    @Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(MtpdailychkService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(MtpdailychkService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");
        long rtnCode = _NORMAL;
        String outMsg = null;

        MtpdlychkI inTrx = new MtpdlychkI();
        MtpdlychkO outTrx = new MtpdlychkO();
        outTrx.setTrx_id(TX_NAME);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        outTrx.setType_id(TRX_OUT);
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, MtpdlychkI.class);

            rtnCode = subMainProc2(inTrx, outTrx);
            if (rtnCode != _NORMAL) {
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
                outTrx.setRtn_code(String.valueOf(rtnCode));
            } else {
                outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
                outTrx.setRtn_mesg(RETURN_MESG_OK);
                retToolDlyChkDao.getCrSession().flush();
            }
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }

    private long subMainProc2(MtpdlychkI inTrx, MtpdlychkO outTrx) {
        long lRc = _NORMAL;
        char actionType = inTrx.getAction_flg().toCharArray()[0];
        switch (actionType) {
            case ACTION_FLG_ADD: {
                lRc = addDailyInfo(inTrx, outTrx);
                if (lRc != _NORMAL) {
                    return lRc;
                }
                break;
            }
            default: {
                outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
                return E_MTPDLYCHK_INVALID_INPUT;
            }
        }
        return lRc;
    }

    private long addDailyInfo(MtpdlychkI inTrx, MtpdlychkO outTrx) {
        long lRc = _NORMAL;
        String evt_seq_id = GUIDGenerator.javaGUID();
        Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
        List<Ret_tool_dly_chk> ret_tool_dly_chk_list = new ArrayList<Ret_tool_dly_chk>();
        
        for(int i=0; i<inTrx.getIary().size(); i++){
        	Ret_tool_dly_chk ret_tool_dly_chk = new Ret_tool_dly_chk();
        	Ret_tool_dly_chkId ret_tool_dly_chkId = new Ret_tool_dly_chkId();
        	ret_tool_dly_chkId.setEvt_seq_id(evt_seq_id);
        	ret_tool_dly_chkId.setData_seq_id(i+1);
        	ret_tool_dly_chk.setId(ret_tool_dly_chkId);
        	ret_tool_dly_chk.setTool_id_fk(inTrx.getTool_id_fk());
        	ret_tool_dly_chk.setData_group(inTrx.getIary().get(i).getData_group());
        	ret_tool_dly_chk.setData_value(inTrx.getIary().get(i).getData_value());
        	ret_tool_dly_chk.setEvt_usr(inTrx.getEvt_usr());
        	ret_tool_dly_chk.setEvt_timestamp(gEvtTimestamp);
        	ret_tool_dly_chk_list.add(ret_tool_dly_chk);
    	}
        
     	if(ret_tool_dly_chk_list.size() > 0){
     		retToolDlyChkDao.saveOrUpdateAll(ret_tool_dly_chk_list);
     	}
        return lRc;
    }
}